home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / session.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  7.8 KB  |  409 lines

  1. /* Session control */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "socket.h"
  7. #include "ftpcli.h"
  8. #include "telnet.h"
  9. #include "icmp.h"
  10. #include "session.h"
  11. #include "cmdparse.h"
  12. #include "timer.h"
  13. #include "proc.h"
  14. #include "tty.h"
  15. #include "commands.h"
  16. #include "hardware.h"
  17.  
  18. struct session *Sessions;
  19. struct session *Current;
  20. struct session *Lastcurr;
  21. extern struct proc *Display;
  22. extern struct session *Command;
  23. int Row;
  24. int Morewait;
  25.  
  26. char Notval[] = "Not a valid control block\n";
  27. static char Badsess[] = "Invalid session\n";
  28. char *Sestypes[] = {
  29.     "",
  30.     "Telnet",
  31.     "FTP",
  32.     "AX25",
  33.     "Finger",
  34.     "Ping",
  35.     "NET/ROM",
  36.     "Command",
  37.     "More",
  38.     "Hopcheck",
  39.     "Post",
  40.         "Rlogin",
  41.     "PPP PAP",
  42. };
  43.  
  44. /* Convert a character string containing a decimal session index number 
  45.  * into a pointer. If the arg is NULLCHAR, use the current default session.
  46.  * If the index is out of range or unused, return NULLSESSION.
  47.  */
  48. struct session *sessptr(cp)
  49. char *cp;
  50. {
  51.     register struct session *sp;
  52.     unsigned int i;
  53.  
  54.     if(cp == NULLCHAR){
  55.         sp = Lastcurr;
  56.     } else {
  57.         i = (unsigned)atoi(cp);
  58.         if(i >= Nsessions)
  59.             sp = NULLSESSION;
  60.         else
  61.             sp = &Sessions[i];
  62.     }
  63.     if(sp == NULLSESSION || sp->type == FREE)
  64.         sp = NULLSESSION;
  65.  
  66.     return sp;
  67. }
  68.  
  69. /* Select and display sessions */
  70. int dosession(argc,argv,p)
  71. int argc;
  72. char *argv[];
  73. void *p;
  74. {
  75.     struct session *sp;
  76.     struct sockaddr fsocket;
  77.     int i,k,s;
  78.     int r,t;
  79.     char *cp;
  80.  
  81. /*    sp = (struct session *)p; */
  82.  
  83.     if(argc > 1){
  84.         if((sp = sessptr(argv[1])) != NULLSESSION){
  85.             go(0,NULL,sp);
  86.         } else
  87.             tprintf("Session %s not active\n",argv[1]);
  88.         return 0;
  89.     }
  90.     tprintf(" #  S#  Type     Rcv-Q Snd-Q State        Remote socket\n");
  91.     for(sp=Sessions; sp < &Sessions[Nsessions];sp++){
  92.         if(sp->type == FREE || sp->type == COMMAND)
  93.             continue;
  94.  
  95.         /* Rcv-Q includes output pending at the screen driver */
  96.         r = socklen(sp->output,1);
  97.         t = 0;
  98.         cp = NULLCHAR;
  99.         if((s = sp->s) != -1){
  100.             i = SOCKSIZE;
  101.             s = sp->s;
  102.             k = getpeername(s,(char *)&fsocket,&i);
  103.             r += socklen(s,0);
  104.             t += socklen(s,1);
  105.             cp = sockstate(s);
  106.         }
  107.         tprintf("%c", (Lastcurr == sp)? '*':' ');
  108.         tprintf("%-3u", (unsigned)(sp - Sessions));
  109.         tprintf("%-4d%-8s%6d%6d %-13s",
  110.          s,
  111.          Sestypes[sp->type],
  112.          r,
  113.          t,
  114.          (cp != NULLCHAR) ? cp : "");
  115.         if(sp->name != NULLCHAR)
  116.             tprintf("%s ",sp->name);
  117.         if(sp->s != -1 && k == 0)
  118.             tprintf("(%s)",psocket(&fsocket));
  119.  
  120.         tprintf("\n");
  121.         if(sp->type == FTP && (s = sp->cb.ftp->data) != -1){
  122.             /* Display data channel, if any */
  123.             i = SOCKSIZE;
  124.             k = getpeername(s,(char *)&fsocket,&i);
  125.             r = socklen(s,0);
  126.             t = socklen(s,1);
  127.             cp = sockstate(s);
  128.             tprintf("    %-4d%-8s%6d%6d %-13s%s",
  129.              s,
  130.              Sestypes[sp->type],
  131.              r,
  132.              t,
  133.              (cp != NULLCHAR) ? cp : "",
  134.              (sp->name != NULLCHAR) ? sp->name : "");
  135.             if(k == 0)
  136.                 tprintf(" (%s)",psocket(&fsocket));
  137.             if(tprintf("\n") == EOF)
  138.                 break;
  139.         }
  140.         if(sp->rfile != NULLCHAR)
  141.             tprintf("    Record: %s\n",sp->rfile);
  142.         if(sp->ufile != NULLCHAR)
  143.             tprintf("    Upload: %s\n",sp->ufile);
  144.     }
  145.     return 0;
  146. }
  147.  
  148. /* Resume current session, and wait for it */
  149. int go(argc,argv,p)
  150. int argc;
  151. char *argv[];
  152. void *p;
  153. {
  154.     struct session *sp;
  155.  
  156.     sp = (struct session *)p;
  157.     if(sp == NULLSESSION || sp->type == FREE || sp->type == COMMAND)
  158.         return 0;
  159.     Current = sp;
  160.     swapscreen(Command,sp);
  161.     psignal(sp,0);
  162.     return 0;
  163. }
  164.  
  165. int doclose(argc,argv,p)
  166. int argc;
  167. char *argv[];
  168. void *p;
  169. {
  170.     struct session *sp;
  171.  
  172.     sp = (struct session *)p;
  173.     if(argc > 1)
  174.         sp = sessptr(argv[1]);
  175.  
  176.     if(sp == NULLSESSION){
  177.         tprintf(Badsess);
  178.         return -1;
  179.     }
  180.     shutdown(sp->s,1);
  181.     return 0;
  182. }
  183.  
  184. int doreset(argc,argv,p)
  185. int argc;
  186. char *argv[];
  187. void *p;
  188. {
  189.     struct session *sp;
  190.  
  191.     sp = (struct session *)p;
  192.     if(argc > 1)
  193.         sp = sessptr(argv[1]);
  194.  
  195.     if(sp == NULLSESSION){
  196.         tprintf(Badsess);
  197.         return -1;
  198.     }
  199.     /* Unwedge anyone waiting for a domain resolution, etc */
  200.     alert(sp->proc,EABORT);
  201.     shutdown(sp->s,2);
  202.     if(sp->type == FTP)
  203.         shutdown(sp->cb.ftp->data,2);
  204.     return 0;
  205. }
  206.  
  207. int dokick(argc,argv,p)
  208. int argc;
  209. char *argv[];
  210. void *p;
  211. {
  212.     struct session *sp;
  213.  
  214.     sp = (struct session *)p;
  215.     if(argc > 1)
  216.         sp = sessptr(argv[1]);
  217.  
  218.     if(sp == NULLSESSION){
  219.         tprintf(Badsess);
  220.         return -1;
  221.     }
  222.     sockkick(sp->s);
  223.     if(sp->type == FTP)
  224.         sockkick(sp->cb.ftp->data);
  225.     return 0;
  226. }
  227.  
  228. struct session *newsession(name,type)
  229. char *name;
  230. int type;
  231. {
  232.     register struct session *sp;
  233.     int i;
  234.  
  235.     for(i=0,sp=Sessions;i < Nsessions;sp++,i++)
  236.         if(sp->type == FREE)
  237.             break;
  238.     if(i == Nsessions)
  239.         return NULLSESSION;
  240.  
  241.     sp->type = type;
  242.     sp->s = -1;
  243.     if(name != NULLCHAR)
  244.         sp->name = strdup(name);
  245.     sp->proc = Curproc;
  246.     Curproc->input =  sp->input = socket(AF_LOCAL,SOCK_STREAM,0);
  247.     Curproc->output = sp->output = socket(AF_LOCAL,SOCK_STREAM,0);
  248.     /* on by default */
  249.     sp->ttystate.crnl = sp->ttystate.edit = sp->ttystate.echo = 1;
  250.     sp->flowmode = 0;    /* Off by default */
  251.     sp->row = MOREROWS;
  252.     sp->morewait = 0;
  253.     newscreen(sp);
  254.     swapscreen(Current,sp);
  255.     Current = sp;
  256.     return sp;
  257. }
  258.  
  259. void freesession(sp)
  260. struct session *sp;
  261. {
  262.     if(sp == NULLSESSION)
  263.         return;
  264.     pwait(NULL);    /* Wait for any pending output to go */
  265.     fflush(stdout);
  266.  
  267.     if(sp->proc1 != NULLPROC)
  268.         killproc(sp->proc1);
  269.     sp->proc1 = NULLPROC;
  270.     if(sp->proc2 != NULLPROC)
  271.         killproc(sp->proc2);
  272.     sp->proc2 = NULLPROC;
  273.  
  274.     free_p(sp->ttystate.line);
  275.     sp->ttystate.line = NULLBUF;
  276.     if(sp->s != -1)
  277.         close_s(sp->s);
  278.     if(sp->record != NULLFILE){
  279.         fclose(sp->record);
  280.         sp->record = NULLFILE;
  281.     }
  282.     free(sp->rfile);
  283.     sp->rfile = NULLCHAR;
  284.     if(sp->upload != NULLFILE){
  285.         fclose(sp->upload);
  286.         sp->upload = NULLFILE;
  287.     }
  288.     free(sp->ufile);
  289.     sp->ufile = NULLCHAR;
  290.     free(sp->name);
  291.     sp->name = NULLCHAR;
  292.     sp->type = FREE;
  293.     close_s(sp->input);
  294.     close_s(sp->output);
  295.     freescreen(sp);
  296.     if(Current == sp){
  297.         Current = Command;
  298.         swapscreen(NULLSESSION,Command);
  299.         alert(Display,1);
  300.     }
  301.     if(Lastcurr == sp)
  302.         Lastcurr = NULLSESSION;
  303. }
  304.  
  305. /* Control session recording */
  306. int dorecord(argc,argv,p)
  307. int argc;
  308. char *argv[];
  309. void *p;
  310. {
  311.     struct session *sp;
  312.  
  313.     sp = (struct session *)p;
  314.     if(sp == NULLSESSION){
  315.         tprintf("No current session\n");
  316.         return 1;
  317.     }
  318.     if(argc > 1){
  319.         if(sp->rfile != NULLCHAR){
  320.             fclose(sp->record);
  321.             free(sp->rfile);
  322.             sp->record = NULLFILE;
  323.             sp->rfile = NULLCHAR;
  324.         }
  325.         /* Open new record file, unless file name is "off", which means
  326.          * disable recording
  327.          */
  328.         if(strcmp(argv[1],"off") != 0){
  329.             if((sp->record = fopen(argv[1],APPEND_TEXT)) == NULLFILE)
  330.                 tprintf("Can't open %s: %s\n",argv[1],sys_errlist[errno]);
  331.             else
  332.                 sp->rfile = strdup(argv[1]);
  333.         }
  334.     }
  335.     if(sp->rfile != NULLCHAR)
  336.         tprintf("Recording into %s\n",sp->rfile);
  337.     else
  338.         tprintf("Recording off\n");
  339.     return 0;
  340. }
  341.  
  342. /* Control file transmission */
  343. int doupload(argc,argv,p)
  344. int argc;
  345. char *argv[];
  346. void *p;
  347. {
  348.     register struct session *sp;
  349.  
  350.     sp = (struct session *)p;
  351.     if(sp == NULLSESSION){
  352.         tprintf("No current session\n");
  353.         return 1;
  354.     }
  355.     if(argc < 2){
  356.         if(sp->ufile != NULLCHAR)
  357.             tprintf("Uploading %s\n",sp->ufile);
  358.         else
  359.             tprintf("Uploading off\n");
  360.         return 0;
  361.     }
  362.     if(strcmp(argv[1],"stop") == 0 && sp->upload != NULLFILE){
  363.         /* Abort upload */
  364.         fclose(sp->upload);
  365.         sp->upload = NULLFILE;
  366.         free(sp->ufile);
  367.         sp->ufile = NULLCHAR;
  368.         killproc(sp->proc2);
  369.         sp->proc2 = NULLPROC;
  370.         return 0;
  371.     }
  372.     /* Open upload file */
  373.     if((sp->upload = fopen(argv[1],READ_TEXT)) == NULLFILE){
  374.         tprintf("Can't read %s: %s\n",argv[1],sys_errlist[errno]);
  375.         return 1;
  376.     }
  377.     sp->ufile = strdup(argv[1]);
  378.     /* All set, invoke the upload process */
  379.     sp->proc2 = newproc("upload",1024,upload,0,sp,NULL);
  380.     return 0;
  381. }
  382.  
  383. /* File uploading task */
  384. void upload(unused,sp1,p)
  385. int unused;
  386. void *sp1;
  387. void *p;
  388. {
  389.     struct session *sp;
  390.     int c;
  391.     int oldf;
  392.  
  393.     sp = (struct session *)sp1;
  394.  
  395.     /* Disable newline buffering for the duration */
  396.     oldf = setflush(sp->s,-1);
  397.     while((c = getc(sp->upload)) != EOF)
  398.         usputc(sp->s,(char)c);
  399.  
  400.     usflush(sp->s);
  401.     setflush(sp->s,oldf);
  402.     fclose(sp->upload);
  403.     sp->upload = NULLFILE;
  404.     free(sp->ufile);
  405.     sp->ufile = NULLCHAR;
  406.     sp->proc2 = NULLPROC;
  407. }
  408.  
  409.